<?php
	/**
	 * 站点管理类
	 */
	 require_once '../lib/base.php';

	class site extends base
	{
		var $db;
		var $msg = '';
		var $page_size = 15;
		var $page = 1;
		var $total_page = 1;

		function site()
		{
			$this->base();
			$this->db = $this->module( 'lib.db' );
			$this->page = isset($_GET['page']) ? intval($_GET['page']) : 1;
			if( $this->page <= 0 ) $this->page = 1;
		}

		function whereclause( $filter=array() )
		{
			if( ! empty($filter) )
			{
				$whereclause = '';
				foreach( $filter as $key=>$val )
				{
					$whereclause .= $key . '=\'' . $val . '\' and ';
				}
				$whereclause = ' where ' . substr($whereclause, 0, -5);
			}
			else
			{
				$whereclause = '';
			}

			return $whereclause;
		}

		function list_site( $filter=array() )
		{
			$whereclause = $this->whereclause( $filter );
			$is_and = empty($whereclause) ? 'where' : 'and';
			$sql = "select meye_site.id,meye_site.name,meye_site.url,meye_site.site_cate_id,meye_site_cate.cname from meye_site,meye_site_cate " . $whereclause . " $is_and meye_site.site_cate_id = meye_site_cate.id";
			$data = $this->page_nav($sql);

			return $data;
		}

		function add_site()
		{
			$url = $_POST['url'];
			$url_hash = md5($url);
			$site = $_POST['site'];
			$ctime = time();
			$user_id = $_SESSION['user_id'];
			$hot_flag = 0;
			$site_cate_id = $_POST['site_cate_id'];

			$sql = "select id from meye_site where name='" . $site . "' or url_hash='". $url_hash ."' ";
			$data =	 $this->db->query( $sql, 'array', true );
			if( ! empty($data['id']) )
			{
				$this->msg = '该站点已经添加过。';
				return false;
			}

			$sql = "insert into meye_site (name,url,url_hash,ctime,user_id,hot_flag,site_cate_id) values ('$site','$url','$url_hash',$ctime,$user_id,$hot_flag,$site_cate_id) ";
			$this->db->query($sql);

			$this->msg = '添加成功。';
			return true;
		}

		function delete_site( $filter=array() )
		{
			$whereclause = $this->whereclause( $filter );

			$sql = "delete from meye_site " . $whereclause;
			$this->db->query($sql);

			$this->msg = '删除成功。';
			return true;
		}

		function modify_site( $filter=array() )
		{
			$whereclause = $this->whereclause( $filter );
			$url = $_POST['url'];
			$url_hash = md5($url);
			$name = $_POST['site'];
			$site_cate_id = $_POST['site_cate_id'];

			$sql = "update meye_site set name='$name', url='$url', url_hash='$url_hash',site_cate_id=$site_cate_id " . $whereclause;
			$this->db->query($sql);

			$this->msg = '修改成功。';
			return true;
		}

		function page_nav( $sql )
		{
			$page_sql = $sql . " limit " . ($this->page-1) * $this->page_size . "," . $this->page_size;
			$data =	 $this->db->query($page_sql, 'all');

			$total_sql = "select count(1) as total_page from ($sql) t";
			$total_data = $this->db->query($total_sql, 'array', true );
			$this->total_page = ceil($total_data['total_page']/$this->page_size);

			return $data;
		}

		function get_menu()
		{
			return '';
		}
	}
?>